package com.warrantyhotel.android.ui.credits;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.GetFreeTable;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.util.BitmapUtils;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.BitmapUtils.OnFetchCompleteListener;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class FreeCreditsDetail extends Activity implements AsyncQueryListener {

	private NotifyingAsyncQueryHandler mHandler;
	private ImageView image;
	private TextView txtTitle;
	private TextView txtDescription;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.free_credits_detail);

		String free_id = getIntent().getStringExtra(Keys._free_credit_id);
		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		image = (ImageView) findViewById(R.id.banner_free_credit_detail);
		txtTitle = (TextView) findViewById(R.id.title_free_credits_detail);
		txtDescription = (TextView) findViewById(R.id.description_free_credits_detail);

		RetrieveFreeFromDBByID(free_id);

	}

	private void RetrieveFreeFromDBByID(String freeID) {
		String[] projectionFree = { GetFreeTable._ID, GetFreeTable.GET_FREE_ID,
				GetFreeTable.ACTION_TEXT, GetFreeTable.ACTION_TITLE,
				GetFreeTable.THUMBNAIL_LINK, GetFreeTable.AWARD_CREDITS,
				GetFreeTable.BANNER_LINK, GetFreeTable.AWARD_UPLOADS };
		mHandler.startQuery(QueriesCodes.FREE_CREDITS,
				GetFreeTable.CONTENT_URI, projectionFree,
				GetFreeTable.GET_FREE_ID + " = '" + freeID + "'", null, null);
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.FREE_CREDITS:
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					String imageUrl = cursor.getString(cursor
							.getColumnIndex(GetFreeTable.BANNER_LINK));
					String title = cursor.getString(cursor
							.getColumnIndex(GetFreeTable.ACTION_TITLE));
					String description = cursor.getString(cursor
							.getColumnIndex(GetFreeTable.ACTION_TEXT));
					BitmapUtils.fetchImage(getApplicationContext(), imageUrl,
							new OnFetchCompleteListener() {
								@Override
								public void onFetchComplete(Object cookie,
										Bitmap result) {
									image.setImageBitmap(result);
								}
							});
					txtTitle.setText(title);
					txtDescription.setText(description);
				}

			}
			break;
		}
	}
}
